// pages/web/web.js
var app = getApp()
import api from '../../utils/api';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.scene) {
      let scene = options.scene;
      console.log(scene);
      scene = scene.replace("|", "&");
      scene = scene.replace("$", "&");
      if (scene.indexOf('&') == -1) {
        scene = decodeURIComponent(scene);
      }
      if (scene.split('&')[0] == 1) {
        console.log(app);
        app.globalData.scanId = scene.split('&')[1];
        if (!wx.getStorageSync('userInfo')) {
          wx.redirectTo({
            url: wx.getStorageSync('login').phoneNumber ? '/pages/loginInfo/loginInfo' : '/pages/login/login'
          })
        } else {
          api.getNewsInfoById({ "newsId": scene.split('&')[1] }).then(newsInfo => {
            if (newsInfo.data.code == 0) {
              if (newsInfo.data.data[0].newsUrl) {
                let openid = newsInfo.data.data[0].newsUrl.indexOf('?') != -1 ? '&openid=' : '?openid=';
                let url = newsInfo.data.data[0].newsUrl + openid + wx.getStorageSync('userId');
                wx.redirectTo({
                  url: '/pages/web/web?url=' + encodeURIComponent(url),
                })
              }
              else {
                wx.switchTab({
                  url: '/pages/index/index'
                });
              }
            } else {
              wx.switchTab({
                url: '/pages/index/index'
              });
            }
          })
        }
      }
    }
    else if (options.url) {
      this.setData({
        url: decodeURIComponent(options.url)
      })
    } else if (options.shareurl) {
      if (!wx.getStorageSync('userInfo')) {
        app.globalData.webUrl = decodeURIComponent(options.shareurl)
        wx.redirectTo({
          url: wx.getStorageSync('login').phoneNumber ? '/pages/loginInfo/loginInfo' : '/pages/login/login'
        })
      } else {
        let url = decodeURIComponent(options.shareurl);
        let openid = url.indexOf('?') != -1 ? '&openid=' : '?openid=';
        url = url + openid + wx.getStorageSync('userId');
        console.log(url);
        this.setData({
          url: url
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let that = this;
    const eventChannel = this.getOpenerEventChannel();
    if (eventChannel.on) {
      eventChannel.on('acceptDataFromOpenerPage', function (data) {
        console.log(data, that);
        that.setData({
          url: data.url
        })
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */

  onShareAppMessage: function (options) {
    let cindex = options.webViewUrl.indexOf('openid');
    let url = options.webViewUrl;
    if (cindex != -1) {
      url = options.webViewUrl.substring(0, cindex - 1);
    }

    //let url = options.webViewUrl.substring(0, 22);
    //if (url == 'https://yty.tourzj.com') {
    console.log(url)
    console.log(encodeURIComponent(url))
    return {
      path: '/pages/web/web?shareurl=' + encodeURIComponent(url)
    }
    //} else {
    //  return {
    //    path: '/pages/index/index'
    //  }
    // }
  }
})